<?php
    include "../database.php";
    db_connect();
    
    $ammount = 10;
    $from = "2009/01/01";
    $to = "2011/01/01";
    $min = 10;
    $max = 100;
    $type = "historical";
    if(isset($_POST['ammount'])) {
	$ammount = mysql_real_escape_string($_POST['ammount']);
    }
    if(isset($_POST['from'])) {
	$from = mysql_real_escape_string($_POST['from']);
    }
    if(isset($_POST['to'])) {
	$to = mysql_real_escape_string($_POST['to']);
    }
    if(isset($_POST['min'])) {
	$min = mysql_real_escape_string($_POST['min']);
    }
    if(isset($_POST['max'])) {
	$max = mysql_real_escape_string($_POST['max']);
    }
    if(isset($_POST['type'])) {
	$type = mysql_real_escape_string($_POST['type']);
    }
    $sql = "SELECT code FROM support";
    $rez = mysql_query($sql);
    $measures = array();
    while($row = mysql_fetch_assoc($rez)) {
	$measures[] = $row['code'];
    }
?>
<form action="randomAmountsGenerator.php" method="post">
	<table>
	<tr><td><label>Kiek skirtingu paramos priemoniu paimti is db(max <?php print count($measures);?>)</label></td><td><input type="text" name="ammount" value="<?php print $ammount;?>"/></td></tr>
	<tr><td><label>Nuo(y/m/d)</label></td><td><input type="text" name="from" value="<?php print $from;?>"/></td></tr>
	<tr><td><label>Iki(y/m/d)</label></td><td><input type="text" name="to" value="<?php print $to;?>"/></td></tr>
	<tr><td><label>Min per diena(menesi)</label></td><td><input type="text" name="min" value="<?php print $min;?>"/></td></tr>
	<tr><td><label>Max per diena(menesi)</label></td><td><input type="text" name="max" value="<?php print $max;?>"/></td></tr>
	<tr><td><label>Prideti kaip istorinius duomenis/nuspejamus</label></td><td>
	<select name="type">
		<option value="historical" <?php if($type == "historical") print "selected"; ?>>Istoriniai</option>
		<option value="predicted" <?php if($type == "predicted") print "selected"; ?>>Nuspejami</option>
	</select></td></tr>
	</table>
	<input type="submit"/>
</form>

<?php
    $records = array();
    $from = strtotime($from);
    $to = strtotime($to);
    while($from <= $to) {
	for($i = 0; $i < $ammount; $i++) {
	    $received = rand($min, $max);
	    if($type == "historical") {
		$administrated = rand(0, $received);
		$records[] = array("date" => date("Y-m-d", $from), "measure_code" => $measures[$i], "received" => $received, "administrated" => $administrated);
	    } else {
		$records[] = array("date" => date("Y-m-d", $from), "measure_code" => $measures[$i], "received" => $received);
	    }
	}
	$from = strtotime(date("Y-m-d", $from) . " +1 month");
    }

    if($type == "historical") {
	$sql = "INSERT INTO historical_quantities (`date`, `measure_code`, `received`, `administrated`) VALUES ";
    } else {
	$sql = "INSERT INTO forecast_quantities (`date`, `measure_code`, `received`) VALUES ";
    }
    $first = "";
    foreach($records as $i) {
	$sql .= $first."('".$i['date']."', '".$i['measure_code']."', '".$i['received']."'";
	if($type == "historical") {
	    $sql .= ", '".$i['administrated']."'";
	}
	$sql .= ")";
	$first = ", ";
    }
    print $sql;
?>